package com.jx.crack;

public class util {

	public static long buf_len(byte[] array) {
		if (array == null) {
			return 0L;
		}
		return array.length;
	}

	public static int buf_to_int8(byte[] array, int index) {
		return 0xFF & array[index];
	}

	public static int buf_to_int16(byte[] array, int index) {
		return (0xFF00 & array[index] << 8) + (0xFF & array[index + 1] << 0);
	}

	public static int buf_reverse_to_int16(byte[] array, int index) {
		return (0xFF00 & array[index + 1] << 8) + (0xFF & array[index] << 0);
	}

	public static int buf_to_int32(byte[] array, int index) {
		return (0xFF000000 & array[index] << 24) + (0xFF0000 & array[index + 1] << 16)
				+ (0xFF00 & array[index + 2] << 8) + (0xFF & array[index + 3] << 0);
	}

	public static int buf_reverse_to_int32(byte[] array, int index) {
		return (0xFF000000 & array[index + 3] << 24) + (0xFF0000 & array[index + 2] << 16)
				+ (0xFF00 & array[index + 1] << 8) + (0xFF & array[index] << 0);
	}

	public static long buf_to_int64(byte[] array, int index) {
		return 0L + (0xFF00000000000000L & array[index] << 56) + (0xFF000000000000L & array[index + 1] << 48)
				+ (0xFF0000000000L & array[index + 2] << 40) + (0xFF00000000L & array[index + 3] << 32)
				+ (0xFF000000L & array[index + 4] << 24) + (0xFF0000L & array[index + 5] << 16)
				+ (0xFF00L & array[index + 6] << 8) + (0xFFL & array[index + 7] << 0);
	}

	public static long buf_reverse_to_int64(byte[] array, int index) {
		return 0L + (0xFF00000000000000L & array[index + 7] << 56) + (0xFF000000000000L & array[index + 6] << 48)
				+ (0xFF0000000000L & array[index + 5] << 40) + (0xFF00000000L & array[index + 4] << 32)
				+ (0xFF000000L & array[index + 3] << 24) + (0xFF0000L & array[index + 2] << 16)
				+ (0xFF00L & array[index + 1] << 8) + (0xFFL & array[index + 0] << 0);
	}

	public static String buf_to_string(byte[] data) {
		String str = "";
		if (data == null) {
			return null;
		} else {
			for (int i = 0; i < data.length; ++i) {
				str = str + Integer.toHexString(0xF & data[i] >> 4) + Integer.toHexString(0xF & data[i]);
			}
		}
		return str;
	}

	public static String buf_reverse_to_string(byte[] data) {
		String str = "";
		if (data == null) {
			return null;
		} else {
			for (int i = 0; i < data.length; ++i) {
				str = Integer.toHexString(0xF & data[i] >> 4) + Integer.toHexString(0xF & data[i]) + str;
			}
		}
		return str;
	}

	public static String buf_to_string(byte[] data, int length) {
		String str = "";
		if (data == null) {
			return null;
		} else {
			if (length > data.length) {
				length = data.length;
			}
			for (int i = 0; i < length; ++i) {
				str = str + Integer.toHexString(0xF & data[i] >> 4) + Integer.toHexString(0xF & data[i]);
			}
		}
		return str;
	}

	public static String buf_reverse_to_string(byte[] data, int length) {
		String str = "";
		if (data == null) {
			return null;
		} else {
			if (length > data.length) {
				length = data.length;
			}
			for (int i = 0; i < length; ++i) {
				str = Integer.toHexString(0xF & data[i] >> 4) + Integer.toHexString(0xF & data[i]) + str;
			}
		}
		return str;
	}

	public static byte[] buf_get_buf(byte[] array, int index, int length) {
		if (array == null || array.length == 0 || length == 0 || index == array.length) {
			return null;
		}
		if ((index + length) > array.length) {// 截取超过长度
			length = array.length - index;
		}
		byte[] tmp = new byte[length];
		System.arraycopy(array, index, tmp, 0, length);
		return tmp;
	}
}
